import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:rebase_project/src/module/TestWidget.dart';
import 'package:provider/provider.dart';
import 'package:logger/logger.dart';
import 'package:oktoast/oktoast.dart';
import 'package:rebase_project/src/provider/MmpNotify.dart';

import 'generated/l10n.dart';
import 'package:caose/caose.dart' show SpUtil,Cache,navigatorContext;
import 'src/base/net/NetHelp.dart';

void main() {
  runZoned(() {
    SpUtil();
    Cache();
    CunwNetHelp.instance;
    Provider.debugCheckInvalidValueType = null;
    ErrorWidget.builder = (FlutterErrorDetails details) {
      Zone.current.handleUncaughtError(details.exception, details.stack);
      return Container();
    };
    runApp(MyApp());
    if (Platform.isAndroid) {
      // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
      SystemUiOverlayStyle systemUiOverlayStyle =
          SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  }, onError: (Object obj, StackTrace stack) {
    Logger().e(obj.toString());
    Logger().e(stack.toString());
  });
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<MmpNotify>(
          create: (c) => MmpNotify(),
        )
      ],
      child: OKToast(
        child: MaterialApp(
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            S.delegate,
            GlobalCupertinoLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: S.delegate.supportedLocales,
          home: Builder(builder: (context) {
            navigatorContext = context;
            return TestWidget();
          }),
          builder: (c, child) {
            return MaxScaleTextWidget(
              child: child,
            );
          },
        ),
      ),
    );
  }
}

class MaxScaleTextWidget extends StatelessWidget {
  final Widget child;

  const MaxScaleTextWidget({
    Key key,
    @required this.child,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var data = MediaQuery.of(context);
    return MediaQuery(
      data: data.copyWith(textScaleFactor: Platform.isAndroid ? 1.0 : 1.0),
      child: child,
    );
  }
}
